2. Primary Key Constraints
A primary key is a column or set
of columns that can be used to uniquely identify a row in a table. If
you use multiple columns to create a primary key, it is referred to as a
composite key.
Data can be repeated in each individual column in the composite key as
long as all of the data elements that make up the composite key are
unique for each row in the table.
You can create the primary key when you create the table, or create it later using the ALTER TABLE
statement. When you create a primary key, SQL Server automatically
creates a unique index to enforce that uniqueness. The unique index will
be clustered unless you specify nonclustered, or if there is already a
clustered index on the table.
To create a primary key on a column when creating a table, you need to specify the PRIMARY KEY keyword following the column definition. Optionally, you can specify CLUSTERED or NONCLUSTERED
to determine the type of index that will be generated. You also have
the option to specify the name of the constraint that will be created by
using the CONSTRAINT keyword followed by
the name of the constraint. If you do not name the constraint, you will
be at the mercy of SQL Server and will end up with an auto-generated
name like PK__Orders__C3905BAF52793849. Listing 5 shows the syntax for creating a primary key when creating a table.
Example 5. Syntax for Creating a Primary Key When Creating a Table
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
--Create the Orders table CREATE TABLE Orders (OrderID int NOT NULL CONSTRAINT PK_ORDERS PRIMARY KEY CLUSTERED)
|
To create a primary key after a table has been created, you can use the ALTER TABLE statement. Listing 6
shows how to create a composite key on a table after the table has
already been created. Since the OrderID will be repeated for each
ProductID contained in the Order- Details table for a single order, you
can uniquely identify the order using the OrderID and the ProductID
combined.
Example 6. Syntax to Create a Composite Key Using the ALTER TABLE Statement
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL DROP TABLE dbo.OrderDetails;
--Create the OrderDetails table CREATE TABLE OrderDetails (OrderID int NOT NULL, ProductID int NOT NULL)
--Alter the OrderDetails table to add a composite key ALTER TABLE OrderDetails ADD CONSTRAINT PK_Order_Details PRIMARY KEY CLUSTERED (OrderID, ProductID)
|
To remove a primary key from a table, you need to issue the ALTER TABLE statement with the DROP CONSTRAINT keywords followed by the name of the primary key, as shown in Listing 7.
Example 7. Syntax to Remove a Primary Key Constraint
ALTER TABLE OrderDetails DROP CONSTRAINT PK_Order_Details
|
3. Unique Constraints
You can use a unique
constraint to maintain distinct values in a column or set of columns
that do not participate in the primary key. As with a primary key, when
you create a unique constraint, SQL Server automatically creates a
unique index to ensure the column values are distinct. Unlike with a
primary key, you can define multiple unique constraints per table. You
can also define a unique constraint on one or more columns that accept NULL values; however, if you define a unique constraint on single column, that column can accept only one NULL value.
To create a unique constraint on a column when creating a table, you need to specify the UNIQUE keyword following the column definition. Optionally, you can specify CLUSTERED or NONCLUSTERED to determine the type of index that will be generated; NONCLUSTERED
is the default. Remember, you can have only one clustered index per
table, so if you are using a clustered index for your primary key, the
index on the unique constraint must be nonclustered. You also have the
option to specify the name of the constraint by using the CONSTRAINT keyword followed by the name of the constraint. Listing 8 shows the syntax for creating a unique constraint when creating a table.
Example 8. Syntax for Creating a Unique Constraint When Creating a Table
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
--Create the Orders table CREATE TABLE Orders (OrderID int NOT NULL CONSTRAINT PK_ORDERS PRIMARY KEY CLUSTERED, OrderNumber int NULL CONSTRAINT UQ_ORDER_NUMBER UNIQUE NONCLUSTERED)
|
To create a unique constraint after you have already created a table, you can use the ALTER TABLE statement. Listing 9 shows how to create a unique constraint using multiple columns on an existing table.
Example 9. Syntax to Create a Unique Constraint Using the ALTER TABLE Statement
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL DROP TABLE dbo.OrderDetails;
--Create the OrderDetails table CREATE TABLE OrderDetails (OrderID int NOT NULL CONSTRAINT PK_ORDERDETAILS PRIMARY KEY CLUSTERED, OrderNumber int NULL, CustomerNumber int NOT NULL)
--Alter the OrderDetails table to add the unique constraint ALTER TABLE OrderDetails ADD CONSTRAINT UQ_ORDER_CUSTOMER_NBR UNIQUE (OrderNumber, CustomerNumber)
|
To remove a unique constraint from a table, you need to issue the ALTER TABLE statement with the DROP CONSTRAINT keywords followed by the name of the unique constraint, as shown in Listing 10.
Example 10. Syntax to Remove a Unique Constraint
ALTER TABLE OrderDetails DROP CONSTRAINT UQ_ORDER_CUSTOMER_NBR